#!/usr/bin/python

import os,re,sys,string


try:
    lang = str(sys.argv[1])
except IndexError:
    lang=""

print "lang: " + lang
if (not(lang == '')):
    langsuffix = "_" + lang
    langparam = "lang=" + lang
    langsuffixparam = "langsuffix=" + langsuffix
else:
    langsuffix = ""
    langparam = ""
    langsuffixparam = ""



if re.compile("Python\Z").search(os.getcwd()):
    os.chdir("..")


def compactXMLsources():
    os.chdir("SourceXML")

    data=''
    for f in os.listdir('.'):
        if not(re.compile("\.xml\Z").search(f)):
               continue

        id = re.compile("\d+").search(f).group(0)
        input=open(f)

        xxx=input.read()
        xxx = re.compile("<\w+").sub("\n\g<0> id='"+id+"' ",xxx,1)
        data = data + xxx
        input.close()

    os.chdir('..')
    output=open('Data' +langsuffix+ '/xmlSources.xml','w')
    output.write('<!-- generated by run.py -->\n<xm_sources>\n'+data+'\n</xm_sources>')
    output.close()


compactXMLsources()

#
# use saxon5 here! saxon6 has some bugs!
#
command = 'saxon5 -o Output' + langsuffix + '/index.html Data' + langsuffix + '/xmlSources.xml XSLT/stylesheet.xslt pwd=${PWD}/ ' + langparam +" "+ langsuffixparam
print "command: " + command
os.system(command)





